package com.example.composedemo.widget

import androidx.compose.foundation.layout.Column
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview

/**
 * compositionLocalOf，provider只会重组访问了current的组件
 * staticCompositionLocalOf，provider内部会全部重组
 */
val localString = staticCompositionLocalOf { "Jetpack Compose" }

@Preview
@Composable
fun CompositionTest() {
    Column {
        Text(text = localString.current, color = MaterialTheme.colorScheme.primary)
        CompositionLocalProvider(
            localString provides "Hello World"
        ) {
            Text(text = localString.current, color = Color.Red, style = MaterialTheme.typography.headlineMedium)
            CompositionLocalProvider(
                localString provides "Hello Compose"
            ) {
                Text(text = localString.current, color = Color.Blue)
            }
        }
        Text(text = localString.current, color = Color.Black)
    }
}